<?php

include APPPATH.'controllers/gnc_mng.php';

class Bur_controller extends Gnc_mng {

	function _navigation() {	
		if ($this->_is_student()) {
			$navigation = $this->load->view('bur/nav/general_student', '', TRUE);
		} else {
			$navigation = $this->load->view('bur/nav/general', '', TRUE);
		}
		return $navigation;
	}
	
	function _acd_year_from_semester($semester)
	{
		return $this->common->get_field_from_key('aka_r_semester', 'key_aka_acd_year', $semester);
	}
	
	function _acd_year_desc($acd_year)
	{
		return $this->common->get_description_from_key('aka_r_acd_year', $acd_year);
	}
	
	function _acd_year_desc_from_semester($semester)
	{
		return $this->common->get_description_from_key('aka_r_acd_year', $this->_acd_year_from_semester($semester));
	}

	function _current_semester()
	{
		$today = unix_to_mysql(now());
		$result = $this->model->get_data_from_query('`nkey`', '`dstart` <= '.$this->db->escape($today).' AND `dend` >= '.$this->db->escape($today), NULL, array('nkey' => 'DESC'), NULL, NULL, 'aka_r_semester');

		reset($result);
		$result = current($result);
		if (isset($result['nkey'])) {
			$result = $result['nkey'];
		} else {
			$result = '';
		}
		return $result;
	}
	
	function _current_semester_desc()
	{
		return $this->_semester_desc($this->_current_semester());
	}
	
	function _semester_desc($semester)
	{
		return $this->common->get_description_from_key('aka_r_semester', $semester);
	}
	
	function _current_month()
	{
		$curr_month = mdate("%m", now());
		if (mdate("%d", now()) > 22)		// if current date > 22, assume process is done for next month
			$curr_month = $curr_month + 1;
		return $curr_month;
	}	
	
	function _is_student()
	{
		return (strcasecmp($this->common->current_role_key(), 'student')==0);
	}
	
	function _students_id()
	{
		$_tabel = $this->model->get_data_from_query('DISTINCT `key`', NULL, NULL, '`key`', NULL, NULL, 'aka_m_student');
		$students = array();
		foreach($_tabel as $idx => $val) {
			$students[] = $val['key'];
		}
		return $students;
	}

	
	function _user_student_id($user) {
		return $this->common->get_field_from_criteria('aka_m_student', 'key', array('key_sys_user' => $user));
	}
	
	function _name($nim) {
		return $this->common->get_field_from_criteria('aka_m_student', 'name', array('key' => $nim));
	}
	
	function _peer_year($nim) {
		return $this->common->get_field_from_criteria('aka_m_student', 'key_aka_peer_year', array('key' => $nim));
	}
	
	function _study_program($nim) {
		$prodi = $this->common->get_field_from_criteria('aka_m_student', 'key_aka_study_program', array('key' => $nim));
		if ($prodi) {
			$prodi = $this->common->get_description_from_key('aka_r_study_program', $prodi);
		}
		return $prodi;
	}

	
	// PAYMENT PLAN
	function _payment_plan_data($nim, $bulan = NULL, $semester = NULL)
	{	
		if (is_null_or_false($bulan)) {
			$bulan = $this->_current_month();
		}
		
		if (is_null_or_false($semester)) {
			$semester = $this->_current_semester();
		}

		$payment_plan_id = $this->_payment_plan_id($nim, $bulan, $semester);
		$_tabel = $this->model->get_data_from_query(array('key_bur_payment_type' => 'biaya', 'total' => 'rp', 'remark' => 'keterangan'), array('id_bur_payment_plan' => $payment_plan_id), NULL, '`id`', NULL, NULL, 'bur_t_detail_payment_plan');
		
		$tabel = array();
		$jumlah = 0;
		foreach ($_tabel as $idx => $val) {
			$tabel[] = array('biaya' => $this->common->get_description_from_key('bur_r_payment_type', $val['biaya']), 'rp' => $val['rp'], 'keterangan' => $val['keterangan']);
			$jumlah = $jumlah + $val['rp'];
		}
		
		$terbilang = $this->common->number_to_words($jumlah, 'id').' rupiah';

	
		$result = array(			
			'tabel' => $tabel,
			'jumlah' => $jumlah,
			'terbilang' => $terbilang,
		);
		
		return $result;
	}

    function _students_with_plan_id($bulan = NULL, $semester = NULL)
    {
        if (is_null($bulan)) {
            $bulan = $this->_current_month();
        }
        
        if (is_null($semester)) {
            $semester = $this->_current_semester();
        }
        
        $_tabel = $this->model->get_data_from_query('`key_aka_student`', array('key_aka_semester' => $semester, 'month' => intval($bulan)), NULL, '`key_aka_student`', NULL, NULL, 'bur_h_payment_plan');
        $students = array();
        foreach($_tabel as $idx => $val) {
            $students[] = $val['key_aka_student'];
        }
        return $students;
    }
	
	function _students_with_processed_plan_id($bulan = NULL, $semester = NULL)
    {
        if (is_null($bulan)) {
            $bulan = $this->_current_month();
        }
        
        if (is_null($semester)) {
            $semester = $this->_current_semester();
        }
        
        $_tabel = $this->model->get_data_from_query('`key_aka_student`', array('key_aka_semester' => $semester, 'month' => intval($bulan), 'key_bur_payment_plan_status' => 'processed'), NULL, '`key_aka_student`', NULL, NULL, 'bur_h_payment_plan');
        $students = array();
        foreach($_tabel as $idx => $val) {
            $students[] = $val['key_aka_student'];
        }
        return $students;
    }
	
	function _confirm_payment($nim, $bulan, $semester)
	{
		if (!$this->_is_student())
			$this->_set_status('payment_plan', $nim, $bulan, $semester, 'processed', 'printed');
	}
	
	function _payment_plan_id($nim, $bulan, $semester) {
		return $this->common->get_id_from_criteria('bur_h_payment_plan', array('key_aka_student' => $nim, 'key_aka_semester' => $semester, 'month' => intval($bulan)));
	}
	
	// DEFERRED PAYMENT REQUEST
	function _deferred_payment_request_data($nim, $bulan = NULL, $semester = NULL)
	{
		if (is_null_or_false($bulan)) {
			$bulan = $this->_current_month();
		}
		
		if (is_null_or_false($semester)) {
			$semester = $this->_current_semester();
		}

		$deferred_payment_request_id = $this->_deferred_payment_request_id($nim, $bulan, $semester);
		$_tabel = $this->model->get_data_from_query(array('key_bur_payment_type' => 'biaya', 'total' => 'rp'), array('id_bur_deferred_payment_request' => $deferred_payment_request_id), NULL, '`id`', NULL, NULL, 'bur_t_detail_deferred_payment_request');
		
		$tabel = array();
		$jumlah = 0;
		foreach ($_tabel as $idx => $val) {
			$tabel[] = array('biaya' => $this->common->get_description_from_key('bur_r_payment_type', $val['biaya']), 'rp' => $val['rp'], 'keterangan' => $val['keterangan']);
			$jumlah = $jumlah + $val['rp'];
		}
		
		$alasan = $this->common->get_field_from_criteria('bur_h_deferred_payment_request', 'reason', array('key_aka_student' => $nim, 'key_aka_semester' => $semester, 'month' => intval($bulan)));

	
		$result = array(			
			'tabel' => $tabel,
			'jumlah' => $jumlah,
			'alasan' => $alasan,
		);
		
		return $result;
	}	
	
	function _students_with_request_id($bulan = NULL, $semester = NULL)
    {       
        if (is_null($bulan)) {
            $bulan = $this->_current_month();
        }
        
        if (is_null($semester)) {
            $semester = $this->_current_semester();
        }
        
		$_tabel = $this->model->get_data_from_query('`key_aka_student`', array('key_aka_semester' => $semester, 'month' => intval($bulan)), NULL, '`key_aka_student`', NULL, NULL, 'bur_h_deferred_payment_request');

        $students = array();
        foreach($_tabel as $idx => $val) {
            $students[] = $val['key_aka_student'];
        }
        return $students;
    }

	function _approve_request($nim, $bulan, $semester)
	{
		if (!$this->_is_student())
			$this->_set_status('deferred_payment_request', $nim, $bulan, $semester, 'approved');
	}
	
	function _unapprove_request($nim, $bulan, $semester)
	{
		if (!$this->_is_student())
			$this->_set_status('deferred_payment_request', $nim, $bulan, $semester, 'unapproved');
	}	
	
	
	function _deferred_payment_request_id($nim, $bulan, $semester) {
		return $this->common->get_id_from_criteria('bur_h_deferred_payment_request', array('key_aka_student' => $nim, 'key_aka_semester' => $semester, 'month' => intval($bulan)));
	}
	
	function _charge_id($nim, $bulan, $semester) {
		return $this->common->get_id_from_criteria('bur_h_charge', array('key_aka_student' => $nim, 'key_aka_semester' => $semester, 'month' => intval($bulan)));
	}		
	

	function _set_status($type, $nim, $bulan, $semester, $new_status, $old_status = NULL) {
		if (strcasecmp($type, 'payment_plan')==0) {
			$table_name = 'bur_h_payment_plan';
			$key = 'key_bur_payment_plan_status';
			$id = $this->_payment_plan_id($nim, $bulan, $semester);
		} else if (strcasecmp($type, 'deferred_payment_request')==0) {
			$table_name = 'bur_h_deferred_payment_request';
			$key = 'key_bur_deferred_payment_request_status';
			$id = $this->_deferred_payment_request_id($nim, $bulan, $semester);
		} else {
			$table_name = false;
		}
	
		if ($table_name) {
			$status = $this->common->get_field_from_criteria($table_name, $key, array('key_aka_student' => $nim, 'key_aka_semester' => $semester, 'month' => intval($bulan)));
			if ((strcasecmp($status, $old_status)==0) || (is_null_or_false($old_status))) {				
				$this->model->update(array($key => $new_status), array('id' => $id), $table_name);
			}
		}
	}

	function reroute() {
		$target = $this->input->post('target');
		$param = $this->input->post('param');
		$param = explode(",", $param);
		foreach ($param as $idx => $val) {
			$param[$idx] = trim($val);
		}

		$reroute_str = $target;
		foreach ($param as $idx => $val) {
			$reroute_str .= '/'.$this->input->post($val);
		}

		redirect($reroute_str);
	}
	
}
